/*
 * Line.java
 *
 * Created on 9 maj 2007, 19:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pl.krakow.ae.knp.wikiboard.gui.drawables;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import pl.krakow.ae.knp.wikiboard.gui.board.Board;
import pl.krakow.ae.knp.wikiboard.utils.GraphicalUtils;

/**
 *
 * @author m
 */
public class Line extends Figure
{
    protected String name;
    private List<Point2D> points;
    private Color color;
    
    /** Creates a new instance of Line */
    public Line(Point2D startingPoint, Color color)
    {
        super();
        this.color = color;
        points = new LinkedList<Point2D>();
        points.add(startingPoint);
    }
    
    protected Line(Line l)
    {
        super();
        this.color = GraphicalUtils.cloneColor(l.color);
        this.points = new LinkedList<Point2D>();
        Iterator<Point2D> it = l.points.iterator();
        while(it.hasNext())
        {
            Point2D cur = it.next();
            this.points.add(new Point2D.Double(cur.getX(), cur.getY()));
        }
    }
    
    public void rotate()
    {
    }
    
    public void drag(MouseEvent event)
    { points.add(event.getPoint()); }
    
    public boolean contains(Point2D p)
    {
        Iterator<Point2D> it = points.iterator();
        while (it.hasNext())
        {
            Point2D cur = it.next();
            if (cur.distanceSq(p) < 300)
                return true;
        }
        return false;
    }
    
    public void draw(Graphics2D g, boolean dragged)
    {
        g.setXORMode(Board.getBgColor());
        if(!dragged)
            g.setPaintMode();
        g.setColor(color);
        
        for (int i = 0; i < points.size()-1; i++)
        {
            Point2D cur = points.get(i);
            Point2D next = points.get(i+1);
            g.draw(new Line2D.Float(cur,next));
        }
        
        if (isSelected)
            drawOutline(g);
    }
    
    private void drawOutline(Graphics2D g)
    {
        double leftmostX = points.get(0).getX();
        double rightmostX = leftmostX;
        double upmostY = points.get(0).getY();
        double downmostY = upmostY;
        
        Iterator<Point2D> it = points.iterator();
        while(it.hasNext())
        {
            Point2D cur = it.next();
            leftmostX = Math.min(leftmostX, cur.getX());
            rightmostX = Math.max(rightmostX, cur.getX());
            upmostY = Math.min(upmostY, cur.getY());
            downmostY = Math.max(downmostY, cur.getY());
        }
        
        Graphics2D g2 = (Graphics2D)g.create();
        g2.setColor(Board.getSelectColor());
        g2.setStroke(new BasicStroke(STROKE_WIDTH));
        g2.drawRect((int)leftmostX - SELECT_PADDING,
                (int)upmostY - SELECT_PADDING,
                (int)(rightmostX-leftmostX) + 2*SELECT_PADDING,
                (int)(downmostY-upmostY) + 2*SELECT_PADDING);
        g2.dispose();
    }
    
    public void move(double x, double y)
    {
        for (int i = 0; i < points.size(); i++)
        {
            double nX = points.get(i).getX();
            double nY = points.get(i).getY();
            nX += x;
            nY += y;
            points.set(i, new Point2D.Double(nX, nY));
        }
    }
    
    public void fill(Color c)
    {
    }
    
    public Figure clone()
    { return new Line(this); }
    
}
